package top.pmwly.web.userController;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import top.pmwly.common.domain.UserSecurity;
import top.pmwly.common.param.UserLoginParam;
import top.pmwly.common.constant.ReslutEnum;
import top.pmwly.common.execption.BaseException;
import top.pmwly.common.reslut.ReslutBean;
import top.pmwly.dao.mapper.SysRoleMapper;
import top.pmwly.dao.pojo.SysRole;
import top.pmwly.dao.pojo.SysUser;
import top.pmwly.service.ISysRoleService;
import top.pmwly.service.ISysUserService;

import javax.validation.Valid;
import java.security.Principal;
import java.util.List;

/**
 * @author Herther
 * @version 1.0.0
 * @ClassName LoginController.java
 * @Description 登录控制
 * @createTime 2021年09月21日 19:22:00
 */
@Api(tags = "LoginController")
@RestController
@Slf4j
@RequestMapping("/user")
public class LoginController {

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysRoleService roleService;

    /**
     * 登录
     * @param userLoginParam
     * @return
     */
    @ApiOperationSupport(author = "Herther")
    @ApiOperation(value = "登录返回token",httpMethod = "POST")
    @PostMapping("/login")
    //@Valid
    public ReslutBean login( @Valid UserLoginParam userLoginParam){
        return sysUserService.login(userLoginParam);
    }

    //@ApiOperation(value = "根据token获取用户主体信息")
    //@GetMapping("/getInfo")
    //public ReslutBean getInfo(Principal principal){
    //    //判断凭证是否为空
    //    if (principal == null){
    //       throw new BaseException(ReslutEnum.FALL_NOT_LOGIN);
    //    }
    //    //根据用户名去查询
    //    UserSecurity info = sysUserService.getInfo(principal.getName());
    //    //将密码设置为空的
    //    SysUser sysUser = info.getSysUser();
    //    sysUser.setPassword("");
    //    info.setSysUser(sysUser);
    //    return ReslutBean.SUCCESS(ReslutEnum.SUCCESS,info);
    //}

    /**
     * 退出登录
     * @return
     */
    @ApiOperation(value = "退出登录")
    @GetMapping("/logout")
    public ReslutBean logout(){
        return ReslutBean.SUCCESS(ReslutEnum.SUCCESS_LOGOUT);
    }

    @ApiOperation(value = "获取用户信息")
    @GetMapping("/getInfo")
    public ReslutBean getInfo(Principal principal){
        //判断凭证是否为空
        if (principal == null){
           throw new BaseException(ReslutEnum.FALL_NOT_LOGIN);
        }
        return sysUserService.getMyinfoVo(principal.getName());
    }



}
